﻿using System;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class LinePLF : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                lblError.Text = "";
                return;
            }
            if (Session["ACL"].ToString().IndexOf("IC1") < 0)
            {
                Response.Redirect("unauthorized.aspx");
            }
            //txtDate.Attributes.Add("onclick", "new Calendar().show(this);");
            if (Session["FLT_DATE"] != null)
                txtDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();
            if (Session["ORI"] != null)
                txtOriCity.Text = Session["ORI"].ToString();
            if (Session["DES"] != null)
                txtDesCity.Text = Session["DES"].ToString();
            //DataTable myTab=new DataTable();
            //SqlCommand cmd = new SqlCommand();
            //try
            //{
            //    cmd.CommandText = "usp_get_city_names_by_lang";
            //    SqlParameter para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());
            //    TinyDAL.Exec(ref cmd, ref myTab);
            //    foreach (DataRow row in myTab.Rows)
            //    {
            //        lstOri.Items.Add(new ListItem(row["name"].ToString(), row["code"].ToString()));
            //        lstDes.Items.Add(new ListItem(row["name"].ToString(), row["code"].ToString()));
            //    }

            //    //cmd.CommandText = "bll_airline_get_names_by_lang";
            //    //TinyDAL.Exec(ref cmd, ref myTab);
            //    //foreach (DataRow row in myTab.Rows)
            //    //{
            //    //    lstAirline.Items.Add(new ListItem( row["code"].ToString()+":"+row["name"].ToString(), row["code"].ToString()));
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    lblError.Text = ex.Message;
            //}
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            int max_opn, paid, pending;
            int total_max = 0, total_paid = 0, total_pending = 0;
            string LastFlt = "";

            TableRow tabRow;
            TableCell tabCell;


            //Table1.Caption = "Line Report";
            tabRow = new TableRow();
            tabCell = new TableHeaderCell();
            tabCell.Text = "Flight";
            tabRow.Cells.Add(tabCell);

            tabCell = new TableHeaderCell();
            tabCell.Text = "Cabin";
            tabRow.Cells.Add(tabCell);

            tabCell = new TableHeaderCell();
            tabCell.Text = "Aircraft Cap.";
            tabRow.Cells.Add(tabCell);

            tabCell = new TableHeaderCell();
            tabCell.Text = "Leg. Max";
            tabRow.Cells.Add(tabCell);

            tabCell = new TableHeaderCell();
            tabCell.Text = "Seg. Max";
            tabRow.Cells.Add(tabCell);

            tabCell = new TableHeaderCell();
            tabCell.Text = "Pending";
            tabRow.Cells.Add(tabCell);

            tabCell = new TableHeaderCell();
            tabCell.Text = "Paid";
            tabRow.Cells.Add(tabCell);

            tabCell = new TableHeaderCell();
            tabCell.Text = "Avail.";
            tabRow.Cells.Add(tabCell);

            Table1.Rows.Add(tabRow);

            //tabRow = new TableRow();
            //tabCell = new TableCell();
            //tabCell.Text = "<hr/>";
            //tabCell.ColumnSpan = 8;
            //tabRow.Cells.Add(tabCell);
            //Table1.Rows.Add(tabRow);

            Session["FLT_DATE"] = txtDate.Text;
            Session["ORI"] = txtOriCity.Text.ToUpper();
            Session["DES"] = txtDesCity.Text.ToUpper();

            DataTable myTab = new DataTable();
            SqlCommand cmd = new SqlCommand();
            try
            {
                cmd.CommandText = "bll_airline_get_line_summary";
                SqlParameter para = cmd.Parameters.AddWithValue("@ori", txtOriCity.Text);
                para = cmd.Parameters.AddWithValue("@des", txtDesCity.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());//只能看本公司情况！
                para = cmd.Parameters.AddWithValue("@date", txtDate.Text);
                //para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());

                TinyDAL.Exec(ref cmd, ref myTab);
                foreach (DataRow row in myTab.Rows)
                {
                    if (LastFlt != row["flt_no"].ToString() && LastFlt != "")
                    {
                        LastFlt = row["flt_no"].ToString();
                        tabRow = new TableRow();
                        tabCell = new TableCell();
                        tabCell.Text = "&nbsp;";
                        tabRow.Cells.Add(tabCell);
                        Table1.Rows.Add(tabRow);
                    }
                    max_opn = Convert.ToInt16(row["class_max"]);
                    pending = Convert.ToInt16(row["pending"]);
                    paid = Convert.ToInt16(row["paid"]);
                    total_max += max_opn;
                    total_pending += pending;
                    total_paid += paid;
                    tabRow = new TableRow();

                    tabCell = new TableCell();
                    tabCell.Text = "<a href=\"flt_seat_ctl.aspx?flt_no=" + row["flt_no"].ToString() + "&flt_date=" + row["flt_date"].ToString() + "\">" + Session["AIRLINE"].ToString() + row["flt_no"].ToString() + "</a>";
                    tabRow.Cells.Add(tabCell);

                    tabCell = new TableCell();
                    tabCell.Text = row["cabin"].ToString();
                    tabRow.Cells.Add(tabCell);

                    tabCell = new TableCell();
                    tabCell.Text = "<img src=\"normal.bmp\" height=\"6px\" width=\"" + row["cabin_cap"].ToString() + "px\"/> " + row["cabin_cap"].ToString();
                    tabRow.Cells.Add(tabCell);

                    tabCell = new TableCell();
                    tabCell.Text = "<img src=\"normal.bmp\" height=\"6px\" width=\"" + row["cabin_max"].ToString() + "px\"/> " + row["cabin_max"].ToString();
                    tabRow.Cells.Add(tabCell);

                    tabCell = new TableCell();
                    tabCell.Text = "<img src=\"normal.bmp\" height=\"6px\" width=\"" + max_opn.ToString() + "px\"/> " + max_opn.ToString();
                    tabRow.Cells.Add(tabCell);
                    if ((pending + paid) * 1.0 / max_opn < 0.1)
                    {
                        tabCell = new TableCell();
                        tabCell.Text = "<img src=\"low.bmp\" height=\"6px\" width=\"" + pending.ToString() + "px\"/> " + pending.ToString();
                        tabRow.Cells.Add(tabCell);

                        tabCell = new TableCell();
                        tabCell.Text = "<img src=\"low.bmp\" height=\"6px\" width=\"" + paid.ToString() + "px\"/> " + paid.ToString();
                        tabRow.Cells.Add(tabCell);
                    }
                    else if ((pending + paid) * 1.0 / max_opn > 1.0)
                    {
                        tabCell = new TableCell();
                        tabCell.Text = "<img src=\"alarm.bmp\" height=\"6px\" width=\"" + pending.ToString() + "px\"/> " + pending.ToString();
                        tabRow.Cells.Add(tabCell);

                        tabCell = new TableCell();
                        tabCell.Text = "<img src=\"alarm.bmp\" height=\"6px\" width=\"" + paid.ToString() + "px\"/> " + paid.ToString();
                        tabRow.Cells.Add(tabCell);
                    }
                    else if ((pending + paid) * 1.0 / max_opn > 0.95)
                    {
                        tabCell = new TableCell();
                        tabCell.Text = "<img src=\"high.bmp\" height=\"6px\" width=\"" + pending.ToString() + "px\"/> " + pending.ToString();
                        tabRow.Cells.Add(tabCell);

                        tabCell = new TableCell();
                        tabCell.Text = "<img src=\"high.bmp\" height=\"6px\" width=\"" + paid.ToString() + "px\"/> " + paid.ToString();
                        tabRow.Cells.Add(tabCell);
                    }
                    else
                    {
                        tabCell = new TableCell();
                        tabCell.Text = "<img src=\"normal.bmp\" height=\"6px\" width=\"" + pending.ToString() + "px\"/> " + pending.ToString();
                        tabRow.Cells.Add(tabCell);

                        tabCell = new TableCell();
                        tabCell.Text = "<img src=\"normal.bmp\" height=\"6px\" width=\"" + paid.ToString() + "px\"/> " + paid.ToString();
                        tabRow.Cells.Add(tabCell);
                    }
                    tabCell = new TableCell();
                    tabCell.Text = "<img src=\"normal.bmp\" height=\"6px\" width=\"" + (max_opn - pending - paid).ToString() + "px\"/> " + (max_opn - pending - paid).ToString();
                    /*
                    if((max_opn - blk - bkd) >=0)
                        tabCell.Text = "<img src=\"normal.bmp\" height=\"6px\" width=\"" + (max_opn - blk - bkd).ToString() + "px\"/> " + (max_opn - blk - bkd).ToString();
                    else
                        tabCell.Text = "<img src=\"alarm.bmp\" height=\"6px\" width=\"" + (-max_opn + blk + bkd).ToString()+ "px\"/> " + (max_opn - blk - bkd).ToString();
                     */
                    tabRow.Cells.Add(tabCell);

                    Table1.Rows.Add(tabRow);

                }
                tabRow = new TableRow();
                tabCell = new TableCell();
                tabCell.Text = "<hr/>";
                tabCell.ColumnSpan = 8;
                tabRow.Cells.Add(tabCell);
                Table1.Rows.Add(tabRow);

                tabRow = new TableRow();
                tabCell = new TableCell();
                tabCell.Text = "Total:";
                tabRow.Cells.Add(tabCell);

                tabCell = new TableCell();
                tabCell.Text = "";
                tabRow.Cells.Add(tabCell);

                tabCell = new TableCell();
                tabCell.Text = "";
                tabRow.Cells.Add(tabCell);
                tabCell = new TableCell();
                tabCell.Text = "";
                tabRow.Cells.Add(tabCell);


                tabCell = new TableCell();
                tabCell.Text = total_max.ToString();
                tabRow.Cells.Add(tabCell);

                tabCell = new TableCell();
                tabCell.Text = total_pending.ToString();
                tabRow.Cells.Add(tabCell);

                tabCell = new TableCell();
                tabCell.Text = total_paid.ToString();
                tabRow.Cells.Add(tabCell);

                tabCell = new TableCell();
                tabCell.Text = (total_max - total_paid - total_pending).ToString();
                tabRow.Cells.Add(tabCell);
                Table1.Rows.Add(tabRow);
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}